<template>
  <div class="app-container">
    <el-card class="product-list-card">
      <!-- Search Input Added Above the Table -->
      <el-row class="search-bar">
        <el-col :span="8">
          <el-input v-model="searchProductName" placeholder="请输入产品名称" clearable @keyup.enter.native="fetchProducts">
            <template #append>
              <el-button type="primary" @click="fetchProducts">搜索</el-button>
            </template>
          </el-input>
        </el-col>
      </el-row>

      <!-- Pagination Moved to the Top -->
      <el-pagination background layout="prev, pager, next, total, sizes" :page-size="pagination.rows"
        :current-page="pagination.page" :total="pagination.total" @current-change="handlePageChange"
        @size-change="handleSizeChange" class="pagination-top" />

      <el-table :data="productList" border style="width: 100%"
        :default-sort="{ prop: 'created_at', order: 'descending' }">
        <!-- Removed Row Selection Column -->
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="产品名称" />
        <el-table-column prop="developer_id" label="开发者ID" width="120" />
        <el-table-column prop="activation_count" label="激活次数" width="100" />
        <el-table-column prop="key_count" label="密钥数量" width="100" />
        <el-table-column prop="created_at" label="创建时间" sortable />
        <el-table-column label="API ID">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="copyApiId(row)">复制</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template #default="{ row }">
            <el-button type="danger" size="small" @click="openDeleteDialog(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

    </el-card>
  </div>
</template>

<script>
import { getProductList, deleteProduct } from '@/api/admin-product'

export default {
  name: 'AdminProductManager',
  data() {
    return {
      searchProductName: '',
      productList: [],
      selectedProducts: [],
      pagination: {
        page: 1,
        rows: 10,
        total: 0
      }
    }
  },
  methods: {
    fetchProducts() {
      if (!this.searchProductName.trim()) {
        this.$message.error('请输入产品名称')
        return
      }
      getProductList(this.searchProductName, this.pagination.rows, this.pagination.page)
        .then(response => {
          this.productList = response.data.products
          this.pagination.total = response.data.total
        })
        .catch(() => {
          this.$message.error('获取产品列表失败')
        })
    },
    handlePageChange(newPage) {
      this.pagination.page = newPage
      this.fetchProducts()
    },
    handleSizeChange(newSize) {
      this.pagination.rows = newSize
      this.pagination.page = 1
      this.fetchProducts()
    },
    copyApiId(row) {
      if (row && row.api_id) {
        navigator.clipboard.writeText(row.api_id)
          .then(() => {
            this.$message.success('API ID 已复制到剪贴板')
          })
          .catch(() => {
            this.$message.error('复制失败')
          })
      } else {
        this.$message.error('无法找到API ID')
      }
    },
    openDeleteDialog(row) {
      this.$confirm(`确定要删除产品 "${row.name}" 吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteProduct(row.id)
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    deleteProduct(productId) {
      deleteProduct(productId)
        .then(() => {
          this.$message.success('删除成功')
          this.fetchProducts()
        })
        .catch(() => {
          this.$message.error('删除失败')
        })
    }
  }
}
</script>

<style scoped>
.product-list-card {
  width: 100%;
  margin: 20px auto;
  padding: 10px;
}

.pagination-top {
  margin-bottom: 15px;
  text-align: right;
}

.search-bar {
  margin-bottom: 20px;
}
</style>
